﻿using Flurl.Http;
using Flurl.Http.Content;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;

namespace ShenNius.Share.Infrastructure.Common
{
    /// <summary>
    /// http帮助类
    /// </summary>
    public class HttpUtil:IDisposable
    {
        #region 示例
        // var url = "http://whois.pconline.com.cn/ipJson.jsp";
        // using (var http = new HttpUtil(url))
        // {
        //     var result = http
        //         .CreateRequest()
        //         .AddString("json", "true")
        //         .GetResult().Result;
        //     Console.WriteLine(result);
           
        //     Console.ReadKey();
        // }
        #endregion

        #region 局部变量
        private string _url = string.Empty;
        private CapturedMultipartContent _content;
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="url">请求地址</param>
        public HttpUtil(string url)
        {
            this._url = url;
            this._content = new CapturedMultipartContent();
        }
        #endregion

        #region 构造请求
        /// <summary>
        /// 构造请求
        /// </summary>
        /// <returns></returns>
        public HttpUtil CreateRequest()
        {
            _content = new CapturedMultipartContent();
            return this;
        }
        #endregion

        #region 添加请求参数
        /// <summary>
        /// 添加请求参数
        /// </summary>
        /// <param name="name">请求参数name</param>
        /// <param name="value">请求参数value</param>
        /// <returns></returns>
        public HttpUtil AddString(string name, string value)
        {
            this._content.AddString(name, value);
            return this;
        }
        #endregion

        #region 添加请求参数StringParts
        /// <summary>
        /// 添加请求参数StringParts
        /// </summary>
        /// <param name="data">new {a = 1, b = 2}</param>
        /// <returns></returns>
        public HttpUtil AddStringParts(object data)
        {
            this._content.AddStringParts(data);
            return this;
        }
        #endregion

        #region 添加上传文件
        /// <summary>
        /// 添加上传文件
        /// </summary>
        /// <param name="name"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public HttpUtil AddFile(string name, string path)
        {
            this._content.AddFile(name, path);
            return this;
        }
        #endregion

        #region 添加Json
        /// <summary>
        /// 添加Json
        /// </summary>
        /// <param name="name"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public HttpUtil AddJson(string name, object data)
        {
            this._content.AddJson(name, data);
            return this;
        }
        #endregion

        #region 添加Hearder
        /// <summary>
        /// 添加Hearder
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public HttpUtil AddHeader(string name, string value)
        {
            this._content.Headers.Add(name, value);
            return this;
        }
        #endregion

        #region 获取返回值
        /// <summary>
        /// 获取返回值
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetResult()
        {
            var result = await this._url.PostMultipartAsync(op => op.Add(this._content));
            return result.GetStringAsync().Result;
        }
        #endregion

        #region 获取返回值
        /// <summary>
        /// 获取返回值
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <returns></returns>
        public T GetResult<T>()
            where T : class, new()
        {
            var result = this.GetResult().Result;
            var model = JsonConvert.DeserializeObject<T>(result);
            if (model == null) return new T();
            return model;
        }
        #endregion

        #region 释放资源
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            _content.Dispose();
        } 
        #endregion
    }
}
